#include <bits/stdc++.h>
using namespace std;
int n,q;
string s[35];
int a,b;
int main(void){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        cin >> s[i];
    }
    scanf("%d",&q);
    while(q--){
        scanf("%d%d",&a,&b);
        //reverse(s[b].begin(),s[b].end());
        int i=0;
        int l=min(s[a].size(),s[b].size());
        int bl=s[b].size();
        int cnt=0;
        while(i<l){
            if(s[a][i]==s[b][bl-i-1]){
                cnt++;
                i++;
            }else{
                break;
            }
        }
        printf("%d\n",cnt);
        //reverse(s[b].begin(),s[b].end());
    }
    return 0;
}